import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import { useUserStore } from '../store/user';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/dashboard',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/login/register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../views/dashboard/index.vue'),
    meta: { title: '工作台', requiresAuth: true }
  },
  {
    path: '/product',
    name: 'Product',
    component: () => import('../views/product/index.vue'),
    meta: { title: '商品管理', requiresAuth: true }
  },
  {
    path: '/product/create',
    name: 'ProductCreate',
    component: () => import('../views/product/create.vue'),
    meta: { title: '新建商品', requiresAuth: true }
  },
  {
    path: '/product/edit/:id',
    name: 'ProductEdit',
    component: () => import('../views/product/create.vue'),
    meta: { title: '编辑商品', requiresAuth: true }
  },
  {
    path: '/product/drafts',
    name: 'ProductDrafts',
    component: () => import('../views/product/index.vue'),
    props: { isDraft: true },
    meta: { title: '草稿箱', requiresAuth: true }
  },
  {
    path: '/product/category',
    name: 'ProductCategory',
    component: () => import('../views/product/category.vue'),
    meta: { title: '商品分组', requiresAuth: true }
  },
  {
    path: '/product/brand',
    name: 'ProductBrand',
    component: () => import('../views/product/brand.vue'),
    meta: { title: '品牌管理', requiresAuth: true }
  },
  {
    path: '/product/tag',
    name: 'ProductTag',
    component: () => import('../views/product/tag.vue'),
    meta: { title: '商品标签', requiresAuth: true }
  },
  {
    path: '/product/attribute',
    name: 'ProductAttribute',
    component: () => import('../views/product/attribute.vue'),
    meta: { title: '商品属性', requiresAuth: true }
  },
  // 内容管理路由
  {
    path: '/content/advertisement',
    name: 'ContentAdvertisement',
    component: () => import('../views/content/advertisement/index.vue'),
    meta: { title: '广告位管理', requiresAuth: true }
  },
  
  // 营销管理路由
  {
    path: '/marketing/coupon',
    name: 'MarketingCoupon',
    component: () => import('../views/marketing/coupon/index.vue'),
    meta: { title: '优惠券管理', requiresAuth: true }
  },
  
  // 订单管理路由
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('../views/orders/index.vue'),
    meta: { title: '订单管理', requiresAuth: true }
  },
  
  // 客户管理路由
  {
    path: '/customer',
    name: 'CustomerList',
    component: () => import('../views/customer/CustomerList.vue'),
    meta: { title: '客户列表', requiresAuth: true }
  },
  {
    path: '/customer/groups',
    name: 'CustomerGroups',
    component: () => import('../views/customer/CustomerGroups.vue'),
    meta: { title: '客户分组', requiresAuth: true }
  },
  {
    path: '/customer/member-settings',
    name: 'MemberSettings',
    component: () => import('../views/customer/MemberSettings.vue'),
    meta: { title: '会员设置', requiresAuth: true }
  },
  
  // 系统管理路由
  {
    path: '/system/role',
    name: 'SystemRole',
    component: () => import('../views/system/role.vue'),
    meta: { title: '角色管理', requiresAuth: true }
  },
  {
    path: '/system/user',
    name: 'SystemUser',
    component: () => import('../views/system/user/index.vue'),
    meta: { title: '账号管理', requiresAuth: true }
  },
  {
    path: '/system/user/create',
    name: 'SystemUserCreate',
    component: () => import('../views/system/user/create.vue'),
    meta: { title: '新建账号', requiresAuth: true }
  },
  {
    path: '/system/user/edit/:id',
    name: 'SystemUserEdit',
    component: () => import('../views/system/user/edit.vue'),
    meta: { title: '编辑账号', requiresAuth: true }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 更新页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 汇流成泽电商管理系统`;
  }
  
  // 判断该路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const userStore = useUserStore();
    if (!userStore.isLoggedIn) {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router; 